package com.siwei.mes.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.util.JsonUtils;
import com.siwei.mes.entity.tgl.TglContract;
import com.siwei.mes.mapper.ContractMapper;
import com.siwei.mes.service.ContractService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * TGL-合同(Contract)表服务实现类
 *
 * @author linzi
 * @since 2023-12-06 20:54:45
 */
@Service
public class ContractServiceImpl implements ContractService {
    @Resource
    private ContractMapper contractMapper;

    @Override
    public Long create(TglContract entity) {
        contractMapper.insertSelective(entity);
        return entity.getCid();
    }

    @Override
    public void update(TglContract entity) {
        TglContract contract = contractMapper.selectByPrimaryKey(entity.getId());
        assertTrue(contract == null, "TGL-合同信息不存在");
        contractMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        TglContract contract = contractMapper.selectByPrimaryKey(id);
        assertTrue(contract == null, "TGL-合同信息不存在");
        contractMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<TglContract> getPage(PageQuery<TglContract, TglContract> query) {
        TglContract searchParam = query.getParams();
        PageInfo<TglContract> list = PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() ->contractMapper.getList(searchParam));
        for (TglContract contract : list.getList()) {
            if(JsonUtils.isJSONString(contract.getExtraInfo())){
                JSONObject jsonObject = JSONUtil.toBean(contract.getExtraInfo(), JSONObject.class);
                if (jsonObject.containsKey("contractName")){
                    contract.setContractName(jsonObject.get("contractName").toString());
                }
            }
        }
        return list;
    }

    @Override
    public TglContract loadById(Long id) {
        TglContract contract = contractMapper.loadById(id);
        assertTrue(contract == null, "TGL-合同信息不存在");
        if(JsonUtils.isJSONString(contract.getExtraInfo())){
            JSONObject jsonObject = JSONUtil.toBean(contract.getExtraInfo(), JSONObject.class);
            if (jsonObject.containsKey("contractName")){
                contract.setContractName(jsonObject.get("contractName").toString());
            }
        }
        return contract;
    }

    @Override
    public List<TglContract> getAll(TglContract entity) {
        List<TglContract> list = contractMapper.getList(entity);
        for (TglContract contract : list) {
            if(JsonUtils.isJSONString(contract.getExtraInfo())){
                JSONObject jsonObject = JSONUtil.toBean(contract.getExtraInfo(), JSONObject.class);
                if (jsonObject.containsKey("contractName")){
                    contract.setContractName(jsonObject.get("contractName").toString());
                }
            }
        }
        return list;
    }
}
